package com.ux.manager
{
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import nexus.base.NXSprite;
	
	public class PopUpManagerBase extends NXSprite
	{
		public function PopUpManagerBase()
		{
			super();
		}
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| private variables
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
		
		private var _popupList : Array = [];
		private var _popupResizeInfo : Array = [];
		private var _modalList : Array = [];
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| children
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| public variables
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/	
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| getter & setter
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| public function
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
		
		/**
		 * PopUpManagerBase - centerPopUp
		 * @ popup  : DisplayObject
		 * @ target : DisplayObject = null
		 * 
		 * void
		 */ 
		public function centerPopUp( popup:DisplayObject, target:DisplayObject=null ):void{
			if( popup && StageManager.stage != null ){
				var stageRectPoint:Rectangle;
				if( target == null ) stageRectPoint = popup.stage.getRect( popup );
				else stageRectPoint = target.getRect( target );
				
				popup.x = stageRectPoint.x + (popup.stage.stageWidth - popup.width)/2;
				popup.y = stageRectPoint.y + (popup.stage.stageHeight - popup.height)/2;
				if( popup as NXSprite ) (popup as NXSprite).apply();
			}
		}
		
		/**
		 * PopUpManagerBase - addPopUp
		 * @ popup	  : DisplayObject
		 * @ isModal  : Boolean
		 * @ isResize : Boolean
		 * @ isTween  : Boolean
		 * @ duration : Number
		 * @ prop 	  : Object
		 * 
		 * return popup
		 */ 
		public function addPopUp( popup:DisplayObject, isModal:Boolean=true, isResize:Boolean=true, isTween:Boolean=true, duration:Number=0.3, prop:Object=null ):DisplayObject{
			
			if( _popupList.indexOf( popup ) > -1 ) return null;
			
			if( popup ){
				var modalCover : NXSprite;
				
				if(isModal){
					modalCover = createCover();
					modalCover.alpha = 0;
					modalCover.setViewSize( StageManager.stage.stageWidth, StageManager.stage.stageHeight );
					StageManager.stage.addChild( modalCover );
					modalCover.apply();
					
					if(isTween) TweenMax.to( modalCover, duration, (prop == null)?{alpha:1}:prop );
				}
				
				_modalList.push( modalCover );
				
				StageManager.stage.addChild( popup );
				if(isResize) StageManager.stage.addEventListener( Event.RESIZE, onPopUpResize );
				if( popup as NXSprite ) (popup as NXSprite).apply();
				
				_popupList.push( popup );
				_popupResizeInfo.push( isResize );
			}
			
			return popup;
		}
		
		/**
		 * PopUpManagerBase - removePopUp
		 * @ popup : DisplayObject
		 * 
		 * void
		 */ 
		public function removePopUp( popup:DisplayObject ):void
		{
			var idx : int = _popupList.indexOf( popup );
			if( idx == -1 ) return;
			
			if( popup && StageManager.stage != null && StageManager.stage.contains( popup ) ){
				StageManager.stage.removeChild( popup );
			}
			if(_modalList[ idx ] && StageManager.stage != null && StageManager.stage.contains( _modalList[ idx ] as NXSprite )){
				StageManager.stage.removeChild( _modalList[ idx ] as NXSprite );
			}
			
			this.apply();
			
			_popupList.splice( idx , 1 );
			_popupResizeInfo.splice( idx , 1 );
			_modalList.splice( idx , 1 );
		}
		
		/**
		 * PopUpManagerBase - hasPopUp
		 * @ popup : DisplayObject
		 * 
		 * return Boolean
		 */ 
		public function hasPopUp( popup:DisplayObject ):Boolean{
			if( popup && StageManager.stage != null && StageManager.stage.contains( popup )  ) return true;
			else return false;
		}
		
		public function setCover( popup:DisplayObject, x:Number, y:Number, width:Number, height:Number ):void{
			
			var modal : NXSprite = _modalList[ _popupList.indexOf( popup ) ];
			if( modal )
			{
				modal.x = x;
				modal.y = y;
				modal.width = width;
				modal.height = height;
				
				modal.apply();
			}
		}
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| protected function
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
		
		/**
		 * Cover
		 * @ default backGroundColor = 0xb7c2ca;
		 * @ default backGroundAlpha = 0.6;
		 */ 
		protected function createCover():NXSprite{
			var cover:NXSprite = new NXSprite();
			cover.borderAllThickness = 0;
			
			cover.backGroundColor = 0xb7c2ca;
			cover.backGroundAlpha = 0.6;
			return cover;
		}
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| event handler
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
		
		/**
		 * StageResize
		 * @ Event.RESIZE
		 */ 
		private function onPopUpResize(event:Event):void{
			
			if( StageManager.stage == null ) return;
			
			var len:int = _popupResizeInfo.length;
			for (var i:int = 0; i < len; i++) 
			{
				if( _popupResizeInfo[ i ] == true && _popupList[ i ] )
				{
					var pop : NXSprite = _popupList[ i ] as NXSprite;
					var stageRectPoint:Rectangle = pop.getRect( pop );
					pop.x = stageRectPoint.x + (pop.stage.stageWidth - pop.width)/2;
					pop.y = stageRectPoint.y + (pop.stage.stageHeight - pop.height)/2;
					
					if(pop as NXSprite) (pop as NXSprite).apply();
				}
			}
			
			len = _modalList.length;
			for( i=0; i<len; i++ )
			{
				if( _modalList[ i ] && StageManager.stage != null && StageManager.stage.contains( _modalList[ i ] as NXSprite ) ){
					_modalList[ i ].setViewSize( StageManager.stage.stageWidth, StageManager.stage.stageHeight );
				}
			}
			
		}
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| customEvent handler
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
		
		/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
		| private function
		|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
	}
}